﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kevin.Comm.Logger;
using Kevin.Comm.Resource;
using Kevin.Comm.Utility;
using Kevin.Service.Client.Restful;
using Kevin.Shop.Business.Core.Components;
using Kevin.Shop.Business.Core.Components.Cookie;
using Kevin.Shop.Business.Core.Components.Sales;
using Kevin.Shop.Business.Products.Components.Category;
using Kevin.Shop.Business.Products.Enumerations;
using Kevin.Shop.Business.Service.Entity.Helper;
using Kevin.Shop.Business.Service.Entity.Products.Enumerations;
using Kevin.Shop.Business.Shopping.Components;
using RiderShop.Mobile.Common.Component;
using RiderShop.Mobile.ServiceEntity.MyAccount;

namespace RiderShop.Mobile.Modle.Purchase
{
    public class ShippingCartModel
    {
        public ShoppingCartInfo CartInfo { get; set; }
        public UserShipingAddressExtend DefaultShippingAddress { get; set; }

        public ShippingCartModel()
        {
        }

        public void UpdataShippingCart(string action, string pId, string qty)
        {
            ShoppingCartHelper.ProcessShoppingCart(action, pId, qty);
            CartInfo = BuildShoppingCartPro();
            DefaultShippingAddress = GetUserDefaultShippingAddress(WebClientContext.Current.UserId);
        }


        /// <summary>
        /// 获取用户默认收货地址，没有默认则返回第一个
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns></returns>
        public UserShipingAddressExtend GetUserDefaultShippingAddress(int userId)
        {
            UserShipingAddressExtend defaultAddress = null;
            List<UserShipingAddressExtend> shippingAddressList = RestfulHelper.Query<UserShipingAddressExtend>(SiteUrl.FormatUrl("SalesManagement_GetUserShipingAddress", userId, 0));
            if (shippingAddressList != null && shippingAddressList.Count > 0)
            {
                var address = shippingAddressList.FirstOrDefault(add => add.IsDefault.HasValue && add.IsDefault.Value);
                defaultAddress = address ?? shippingAddressList[0];
            }
            return defaultAddress;
        }

        public static ShoppingCartInfo BuildShoppingCartPro()
        {
            var info = new ShoppingCartInfo();
            var list = ShoppingCartHelper.BuidShoppCartForCookie();
            if (list != null && list.Count > 0)
            {
                string url = SiteUrl.FormatUrl("Service_WebSite_BuildShoppingCartPro");
                info = RestfulHelper.QueryWithSingle<ShoppingCartInfo>(url, list);
            }
            foreach (var item in info.ProductList)
            {
                item.ImageName = SiteUrl.FormatProductImageUrl(ImageType.Size60, item.ProductId, item.ImageName);
            }
            return info;
        }

        public string PreCheckProduct()
        {
            CartInfo = BuildShoppingCartPro();

            if (CartInfo.ProductList.Any(x => x.ProductType == ProductInfoType.Product) &&
                CartInfo.ProductList.Any(x => x.ProductType == ProductInfoType.GiftCard || x.ProductType == ProductInfoType.VipPointCard))
            {
                return "礼品卡或会员卡只能单独下单，请重新选择商品。";
            }
            foreach (var item in CartInfo.ProductList)
            {
                if (item.Quantity > item.LimitQuantity)
                {
                    return string.Format("您购买的商品({0})数量不能大于{1},给您带来不便，敬请谅解！", item.Title, item.LimitQuantity);
                }
                else if (item.TotalInventory <= 0)
                {
                    return string.Format("您购买的商品({0})暂时无货，给您带来不便，敬请谅解！", item.Title);
                }
                else if (item.Quantity > item.TotalInventory)
                {
                    return string.Format("您购买的商品({0})数量不能大于{1},给您带来不便，敬请谅解！", item.Title, item.TotalInventory);
                }
            }
            return string.Empty;
        }

        public ShoppingCartAmountInfo GetShippingCartInfo()
        {
            CartInfo = BuildShoppingCartPro();
            //商品信息
            //this.RepProductList.DataSource = CartInfo.CommonProductList;
            //this.RepProductList.DataBind();
            //string strRepeaterProductList = RenderHtmlManager.RenderToHtml(this.RepProductList);
            //赠品信息
            //this.RepGiftList.DataSource = CartInfo.GiftList;
            //this.RepGiftList.DataBind();
            //string strRepeaterGiftList = RenderHtmlManager.RenderToHtml(this.RepGiftList);
            var cartView = new ShoppingCartAmountInfo(CartInfo);
            //cartView.StrRepeaterGiftList = strRepeaterGiftList;
            return cartView;
        }

    }
}
